package com.gopay.solution.service.solu.impl;

import java.io.Serializable;
import java.math.BigDecimal;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.core.dic.manager.CpsTransInfoQueryManager;
import com.gopay.common.core.dic.manager.DicPayChannelManager;
import com.gopay.common.domain.cps.CpsTransInfo;
import com.gopay.common.domain.dic.DicPayChannel;
import com.gopay.common.domain.poundage.PoundageReq;
import com.gopay.common.domain.solution.SolutionRequest;
import com.gopay.common.domain.solution.SolutionResponse;
import com.gopay.common.exception.validator.GopayCheckException;
import com.gopay.common.constants.solution.SolutionTypeConstant;

/**
 * 示例
 * 
 * @SsTransFeeServiceImpl.java
 * @author MA HUAN
 * @2013-3-26 下午6:49:06 www.gopay.com.cn Inc.All rights reserved.
 */

@Service(value = "ssTransFeeService")
public class SsTransFeeServiceImpl extends SolutionServiceImpl {

    @Resource(name = "dicPayChannelManager")
    private DicPayChannelManager dicPayChannelManager;
    @Resource(name = "cpsTransInfoQueryManager")
    private CpsTransInfoQueryManager cpsTransInfoQueryManager;

    @Override
    public String getSolutionType() {
        return SolutionTypeConstant.TRANS_FEE;
    }

    public SolutionRequest beforeProcess(SolutionRequest request) throws Exception{

       

        Serializable input = request.getRequest();
        if (null == input || !(input instanceof PoundageReq)) {
            return null;
        }

        PoundageReq req = (PoundageReq) input;
        String payChann = req.getPayChannel();
        String acctType = req.getAcctType();
        IntTxnCd intTxnCd = req.getIntTxnCd();
        BigDecimal amt = req.getAmount();

        if (!StringUtils.equals("02", acctType) && !StringUtils.equals("01", acctType)) {
            throw new GopayCheckException(ProcCodeConstants.PROC_CODE_100E1027);
        }
        
        if(null == amt || amt.compareTo(new BigDecimal("0.00")) < 0){
            throw new GopayCheckException(ProcCodeConstants.PROC_CODE_100E1041);
        }
        
        if(StringUtils.isEmpty(payChann)){
            throw new GopayCheckException(ProcCodeConstants.PROC_CODE_100E5006);
        }
        
        if(null == intTxnCd){
            throw new GopayCheckException(ProcCodeConstants.PROC_CODE_100E7006);
        }
        
        DicPayChannel p = dicPayChannelManager.get(payChann);
        if (null == p) {
            throw new GopayCheckException(ProcCodeConstants.PROC_CODE_100E5006);
        }

        CpsTransInfo info = cpsTransInfoQueryManager.get(intTxnCd.value);
        if (null == info) {
            throw new GopayCheckException(ProcCodeConstants.PROC_CODE_100E7006);
        }
        return request;
    }

    public SolutionResponse afterProcess(SolutionResponse response) {

        

        return response;
    }
}
